import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-06-06
 * Time: 10:58
 */

@WebServlet("/save")
public class Save extends HttpServlet {

    ObjectMapper objectMapper = new ObjectMapper();



    //传入表的名字，和username
    public static boolean isExistence(String tableName, String username) {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/Loves?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("mmsszsd666");//这是俺的微信号，欢迎添加，相互学习！
        boolean ret = false;
        try {
            Connection connection = dataSource.getConnection();
            String sql = "select * from " + tableName + " where name = '" + username + "';";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);

            //这里的Set并不是，对象为Love的Set集合，而是一个迭代器！
            ResultSet set = preparedStatement.executeQuery();
            //迭代他（是next方法而不是hasNext）
            ret = set.next();
            set.close();
            preparedStatement.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ret;//true 存在，false不存在

    }
    public static void insert(String tableName, String key, String value) {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/Loves?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("mmsszsd666");//这是俺的微信号，欢迎添加，相互学习！
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
            String sql = "insert into " + tableName + " values(?, ?)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, key);
            preparedStatement.setString(2, value);
            preparedStatement.executeUpdate();
            preparedStatement.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = objectMapper.readValue(req.getInputStream(), User.class);
        resp.setContentType("text/html; charset=utf8");
        String username = user.name;//获取key对应值
        String password = user.password;
        if(isExistence("users", username)) {
            resp.getWriter().write("No");//存在此用户了！
        }else {
            insert("users", username, password);
        }
    }



}
